var gastos = {
		init:function(){$.getJSON("tipodocumentos/listatipodocumentos", function(listatipodocumento) {
  				setupGrid(listatipodocumento);
 			});
			var gasto = this;
			function setupGrid(listatipodocumento){
			$('#gastos').jqGrid({
	           url:'gastos/index',
	           datatype:'json',
	           mtype:'POST',
	           colNames:['id','Tipo documento','Serie','Numero','Razon Social','Descripcion','Igv','Monto','Fecha','Creado','Estado'],
	           colModel:[
	               {name:"data[Gasto][id]",hidden:true},
	               {name:"data[Gasto][tipodocumento_id]",index:'Tipodocumento.descripcion',width:60,sortable:false,editable:true,edittype:"select", editrules:{required:true},editoptions:{value:listatipodocumento},formoptions:{ rowpos:1, elmprefix:"(*)"},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Gasto][serie]",index:'Gasto.serie',width:30,align:'left',sortable:true,editable:true,editoptions:{maxlength:3},formoptions:{ rowpos:2, elmprefix:"(*)"},editrules:{required:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Gasto][numero]",index:'Gasto.numero',width:40,align:'left',sortable:true,editable:true,editoptions:{maxlength:20, size:25},formoptions:{ rowpos:3, elmprefix:"(*)"},editrules:{required:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Gasto][razon]",index:'Gasto.razon',width:100,align:'left',sortable:true,editable:true,editoptions:{maxlength:20, size:25},formoptions:{ rowpos:4, elmprefix:"(*)"},editrules:{required:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Gasto][descripcion]",index:'Gasto.descripcion',width:140,align:'left',sortable:true,editable:true,editoptions:{maxlength:40, size:25},formoptions:{ rowpos:5, elmprefix:"(*)"},editrules:{required:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Gasto][igv]",index:'Gasto.igv',width:40,align:'right',sortable:true,editable:true,editoptions:{maxlength:20, size:25},formoptions:{ rowpos:6, elmprefix:"(*)"},editrules:{required:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Gasto][monto]",index:'Gasto.monto',width:50,align:'right',sortable:true,editable:true,editoptions:{maxlength:20, size:25},formoptions:{ rowpos:7, elmprefix:"(*)"},editrules:{required:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Gasto][fecha]",index:'Gasto.fecha',width:50,editoptions:{size:15, maxlength:10},
	            	   editoptions:{size:12, 
	            	   dataInit:function(el){ $(el).datepicker({dateFormat:'yy-mm-dd'}); },            	 
	            	   defaultValue: function(){ 
	            		   var currentTime = new Date(); 
	            		   var month = parseInt(currentTime.getMonth() + 1); 
	            		   month = month <= 9 ? "0"+month : month; var day = currentTime.getDate(); 
	            		   day = day <= 9 ? "0"+day : day; var year = currentTime.getFullYear(); 
	            		   return year+"-"+month + "-"+day; } },	            		   
	            	   formoptions:{ rowpos:8, elmprefix:"(*)",elmsuffix:" yyyy-mm-dd" },	            	   
	            	   editable:true,sorttype:"date",editrules:{edithidden:true,date:true,required:false}},
            	   {name:"data[Gasto][created]",index:'Gasto.created',width:50,align:'left',sortable:true,editable:false,searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
	               {name:"data[Gasto][estado_id]",index:'Estado.descripcion',width:40,	align:'left',editable: true,edittype:"select",editoptions:{value:"1:Activo;2:Desactivo"},formoptions:{ rowpos:9, elmprefix:"(*)"},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}}               
	           ],
			   pager:$('#gastos-pager'),
	           rowNum:10,
	           rowList:[10,20,30],
	           mtype: "POST",
	           rownumbers: true, 
	           viewrecords:true,
	           caption:'Gastos',
	           height:'230',
	           sortname: "Gasto.fecha",
	           sortorder: "desc", 
	           editurl: 'gastos/index'       
	       });
		
		  $('#gastos').navGrid("#gastos-pager",{view:true,edit:true,add:true,del:true},
			{clearAfterEdit:false,reloadAfterSubmit:true, bottominfo:"Campos marcados con (*) son requeridos", closeOnEscape:true, afterSubmit:gasto.afterSubmit},
			{clearAfterAdd:true,reloadAfterSubmit:true, bottominfo:"Campos marcados con (*) son requeridos", closeOnEscape:true,afterSubmit:gasto.afterSubmit},
			{width:270,reloadAfterSubmit:true,closeOnEscape:true},
			{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew'],multipleSearch:true,closeOnEscape:true});	       
		   $("#gastos").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false});
	       jQuery("#gastos").jqGrid('setGridWidth',$('#RightPane').width()-50);
		}},
		//afterSubmit: function(response){var respuesta = JSON.parse(response.responseText); return [respuesta.respuesta,respuesta.mensaje];}
};